<?php

namespace App\Controller\App;

use App\Base\BaseConfig;
use App\Base\BaseController4Restful;
use App\Consts\Citys;
use App\Model\BannerModel;
use App\Model\CategoryModel;
use App\Repository\BannerRepository;
use App\Repository\CategoryRepository;
use App\Repository\ConfigRepository;
use App\Repository\StatisticRepository;
use Symfony\Component\Routing\Annotation\Route;

class ConfigController extends BaseController4Restful
{
    /**
     * @Route("/app/config", name="AppGetConfig")
     */
    public function AppGetConfig(
        CategoryRepository $categoryRepository
        ,ConfigRepository $configRepository
        ,BannerRepository $bannerRepository
        ,StatisticRepository $statisticRepository
    )
    {
        $cat = $categoryRepository->findBy(array("parent" =>null,"enable"=>1),array("sort"=>"ASC"));
        $data["cats"] = $cat;

        $conf = $configRepository->findAll();
        $config = [];
        foreach ($conf as $row) {
            $config[$row->getK()] = $row->getV();
        }
        $data["conf"] = $config;

//        $city = new Citys();
//        $data["citys"] = $city->getData();
        $data["citys"] = [];

        //banner设置
        //---首页banner
        $data["bannerIndex"] = $bannerRepository->findBy(array("cat"=>BaseConfig::__CAT_BANNER_INDEX__),array("sort"=>"ASC"));
        //---需求大厅banner
        $data["bannerTask"] = $bannerRepository->findBy(array("cat"=>BaseConfig::__CAT_BANNER_TASK__),array("sort"=>"ASC"));
        //---接单banner
        $data["bannerMatch"] = $bannerRepository->findBy(array("cat"=>BaseConfig::__CAT_BANNER_MATCH__),array("sort"=>"ASC"));

        //保证金支付金额设置
        $money = $this->myExplodeNumbers($config["INSURE_MONEY"]);
        $data["moneySet"] = $money;

        //平台数据
        $statistic = $statisticRepository->find(1);
        $data["statistic"] = $statistic;

        $this->setGroups("app");
        return $this->json_out(200,"数据获取成功",$data);
    }

    /**
     * @Route("/app/hot/cat", name="AppGetHotCategory")
     */
    public function AppGetHotCategory(CategoryRepository $categoryRepository){
        $data = $categoryRepository->findBy(array("isHot"=>1,"enable"=>1),array("sort"=>"ASC"));
        return $this->json_out(200,"OK",$data);
    }

    /**
     * @Route("/app/banners", name="AppGetBanners")
     */
    public function AppGetBanners(BannerRepository $repository){
        if(!$position = $this->getParamRequired("position")){return $this->json_out(400,"参数缺失");}
        $data = $repository->findBy(array("cat"=>$position),array("sort"=>"ASC"));
        return $this->json_out(200,"OK",$data);
    }

    /**
     * @Route("/app/category", name="AppGetCats")
     */
    public function AppGetCats(CategoryRepository $repository){

//        $data = $model->getRootCategory();
        $data = $repository->findBy(array("parent"=>null));
        $this->setGroups("app");
        return $this->json_out(200,"OK",$data);
    }

    /**
     * @Route("/app/charge/config", name="AppGetChargeConfig")
     */
    public function AppGetChargeConfig(ConfigRepository $configRepository){

//        $data = $model->getRootCategory();

        $conf = $configRepository->findAll();
        $config = [];
        foreach ($conf as $row) {
            $config[$row->getK()] = $row->getV();
        }
        //保证金支付金额设置
        $money = $this->myExplodeNumbers($config["MONEY_SET"]);
//        dd($money);
        //资金处理
        $moneySet = [];
        $cound = floor(count($money)/2);
//        dd($cound);
        for ($i= 0;$i<$cound;$i++) {
            $moneySet[]=[$money[$i * 2],$money[$i * 2 + 1]];
        }

//        foreach ($money as $one) {
//            $moneySet[]=[$one*10,$one];
//        }

        $data["moneySet"] = $moneySet;
        return $this->json_out(200,"OK",$data);
    }
}
